package demo.uhf.com.androiduhf;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by neo on 2017/12/14.
 */

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private static final Byte MEM_EPC = 0x01;// epc区

    EditText mEdtInput;
    TextView mTvData;

    String epc;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setting);


        initView();
        initData();
    }


    private void initView() {
        mEdtInput = (EditText) findViewById(R.id.edt_input);
        findViewById(R.id.btn_write).setOnClickListener(this);
        findViewById(R.id.btn_find).setOnClickListener(this);
        mTvData = (TextView) findViewById(R.id.tv_data);
    }


    private void initData() {

    }

    private String getInputData() {
        String data = mEdtInput.getText().toString().trim();
//        if (TextUtils.isEmpty(data)) {
//            data = mEdtInput.getHint().toString().trim();
//        }
        return data;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_write:
                if (TextUtils.isEmpty(epc)) {
                    showToast("请先寻卡");
                    return;
                }

                String inputData = getInputData();
                if (TextUtils.isEmpty(inputData)) {
                    showToast("请输入数据");
                    return;
                }

                int result = UhfHelper.getInstance().write("00000000", epc,
                        MEM_EPC, 2, 6, inputData);
                if (result == 0) {
                    showToast("成功写入EPC区");
                    DevBeep.PlayOK();
                } else {
                    String errorDescription = UhfHelper.getInstance().mIso1800_6C.getErrorDescription(result);
                    showToast("写入失败" + errorDescription);
                    DevBeep.PlayErr();
                }
                break;
            case R.id.btn_find:
                startFind();
                break;
            default:
                break;
        }
    }

    boolean isFind;

    private void startFind() {
        isFind = false;
        UhfHelper.getInstance().startLooper(new UhfHelper.IUhfListener() {
            @Override
            public void doInventory(String data) {
                if (!TextUtils.isEmpty(data)) {
                    isFind = true;
                    epc = data;
                    String dataStr = UhfHelper.getInstance().getEpcData(epc);
                    mTvData.setText("epc:" + epc
                            + " \n data: " + dataStr);
                    UhfHelper.getInstance().stopLooper();
                }
            }
        });
    }

    Toast mToast;

    private void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();
    }
}
